// pages/con_order/con_order.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hostUrl: app.globalData.hostUrl,
    shipping_method:'邮寄到家',
    recommended_code:'',
    note:'',
    shippingMethodActive:2,
    address:'',
    addressId:'',
    key:'',
    count:'',
    type:'',
    productid:'',
    pay_method:'2',
    button:'true',
    pay: [
      { name: '2', value: '微信支付', img: '../images/wechat.png', checked: 'true'},
      { name: '4', value: '蓝旗币', img: '../images/wechat.png'},

      // { name: '2', value: '钱包支付', img: '../images/积分.png' },
      // { name: '3', value: '佣金支付', img: '../images/报单.png' },
     
    ],
    payway:2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      id:options.id
    })
    that.getuser();
    // console.log(that.data.Category,'级别id')
    var userId = wx.getStorageSync('userId');
    if (userId != null && userId > 0 && userId != '') {
      that.getCartList();//读取购物车
      // that.dTotal();//读取金额
      // that.getAddressList();
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '先登录',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/mine/mine',
            })
          } else if (res.cancel) {
          }
        }
      })
    }

  },
  //支付方式选择
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      pay_method: e.detail.value,
    })
    console.log(this.data.pay_method,'支付方式')
  },
  //获取用户级别信息
  // memberlelev:function(){
  //   var that=this;
   
  //   wx.request({
  //     url: app.globalData.apiUrl + 'User/getUserlevel',
  //     data: {
  //       // opt: 'getCartList',
  //       userlelevid: that.data.userlelevid,
  //     },
  //     success: function (res) {
  //       if (res.data != null) {
  //         that.setData({
  //           userlelevinfo: res.data,
  //         })
  //         console.log(that.data.dTotal,'原金额')
  //         console.log(that.data.dTotal * (that.data.userlelevinfo.xiaofeiyouhui / 100, '优惠金额'))
  //         that.setData({
  //           dTotal: that.data.dTotal - (that.data.dTotal * (that.data.userlelevinfo.xiaofeiyouhui / 100))
  //         })
  //       }
  //     }
  //   })
  // },
  //获取用户积分
  getuser:function(){
    var that=this;
    wx.request({
      url: app.globalData.apiUrl +'User/getUser',
      data: {
        // opt: 'getCartList',
        userId: wx.getStorageSync('userId'),
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            userscores: res.data.Scores,
            usercommission:res.data.Commission,
          })
        }
      }
    })
  },
  getCartList: function () {//读取购物车
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/get_card',
      data: {
        userId: wx.getStorageSync('userId'), 
        card_id:that.data.id,
        // productid:152,
        // type:that.data.type,
        
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        if (res.data != null) {
          that.setData({
            cartList: res.data,
            // zongcount: res.data.zongcount,
            // dTotal: res.data.dTotal + res.data.yunfei,
            // yunfei: res.data.yunfei,
            // LevelId: res.data.LevelId
          })
        }
      }
    })
  },
  dTotal: function () {//读取金额
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/dTotal',
      data: {
        // opt: 'dTotal',
        userId: wx.getStorageSync('userId'),
        key: that.data.key,
        count: that.data.count,
        cart_id: that.data.productid,
        type: that.data.type,
        Category: that.data.Category,
        levelsid: wx.getStorageSync('userlelevid'),

      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            dTotal: res.data.dTotal + that.data.yunfei,
            // marketPriceTotal: res.data.marketPriceTotal,
            quantity: res.data.quantity,
          })
        }
      }
    })
  },
  //读取默认地址
  getAddressList: function () {//读取收货地址
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/getAddressList',
      data: {
        // opt: 'getAddressList',
        userId: wx.getStorageSync('userId'),
        // where:' and is_default=1',
      },
      method:'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        if (res.data != "") {
          that.setData({
            address: res.data[0].Detailed,
            addressId: res.data[0].Id,
          })
        }
      }
    })
  },
  //选择地址后读取地址信息
  getxuanAddressList: function (addrid) {//读取收货地址
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/getxuanAddressList',
      data: {
        // opt: 'getAddressList',
        addrid: addrid,
      },
      method:'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res,'地址信息')
        if (res.data != null) {
          that.setData({
            address: res.data.Detailed,
            addressId: res.data.Id,
          })
        }
      }
    })
  },
  formSubmit: function (e) {//提交
    // console.log(e)
    // return ;
    var that = this;
    if (that.data.pay_method == "") {
      
        wx.showToast({
          title: '请选择支付方式',
          icon: 'none',
          duration: 1500
        })
        return;
    }
    if (that.data.pay_method == 4) {
      console.log(111, '蓝旗币计算')
      if (Number(that.data.cartList[0]['Price']) > Number(that.data.userscores)) {
        wx.showToast({
          title: '蓝旗币不足!',
          icon: 'none',
          duration: 1500
        })
        return;
      }
    }
    // if (that.data.pay_method == 3) {
    //   // console.log(111,'积分判断')
    //   if (that.data.dTotal > that.data.usercommission) {
    //     wx.showToast({
    //       title: '佣金不足!',
    //       icon: 'none',
    //       duration: 1500
    //     })
    //     return;
    //   }
    // }

        that.setData({
          button:'false',
        })
        wx.request({
          url: app.globalData.apiUrl +'Hotel/AddOrder', //接口地址
          data: {
            // opt: 'addOrder',
            userId: wx.getStorageSync('userId'),
            // addressId: e.detail.value.addressId,
            quantity_sum: e.detail.value.quantity_sum,
            price_sum: e.detail.value.price_sum,//总金额
            // key: that.data.key,
            // count: that.data.count,
            card_id: that.data.id,
            // type: that.data.type,
            payway: that.data.pay_method,
            // Category: that.data.Category,
            // levelsid: wx.getStorageSync('userlelevid'),
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res)
            // return ;
            if (res.data.status > 0) {
              if (res.data.PayWay==2){
                that.pay(res.data.orderid, res.data.PayWay);

              }else{
                wx.navigateTo({
                  url: '../new_inter/zhifu/zhifu?orderid=' + res.data.orderid + '&type=' + res.data.message,
                })
              }
              // wx.showToast({
              //   title: '提交成功!',//这里打印出登录成功
              //   icon: 'success',
              //   duration: 1000,
              //   success:function(){
              //     wx.navigateTo({
              //       url: '/pages/personal/lvjuka/lvjuka',
              //     })
              //   }
              // })
            } else {
              wx.showToast({
                title: '提交失败!',
                icon: 'loading',
                duration: 1500
              })
            }
          }
        })
    
  },
  pay: function (id,payway) {//支付
    var that = this;
    wx.request({
      url: app.globalData.apiUrl +'Pay/lvju_GetUnifiedOrderResult',
      data: {
        // opt: 'GetUnifiedOrderResult',
        userId: wx.getStorageSync('userId'),
        id: id,//订单id
        // typeId: 2,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res,'支付返回信息')
        if (res.data != null && payway==2) {
          
          wx.requestPayment({            
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr,
              'package': res.data.package,
              'signType': 'MD5',
              'paySign': res.data.paySign,
              'success': function (res) {
                console.log(res)
                wx.navigateTo({
                  url: '../personal/lvjuka/lvjuka',
                })
              },
              'fail': function (res) {
                wx.navigateTo({
                  url: '../personal/lvjuka/lvjuka',
                })
              },
              'complete': function (res) {
                wx.navigateTo({
                  url: '../personal/lvjuka/lvjuka',
                })
                }
          })
        }else{
          console.log("非微信支付方式")
          wx.showToast({
            title:res.data.msg,
            icon:"none",
            duration:2000,
            success:function(){
              setTimeout(function () {
                //要延时执行的代码
                wx.redirectTo({
                  url: '/pages/order/order',
                })   
              }, 2000) //延迟时间
            },
          }) 
        }
      }
    })
  },
  shippingMethod:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    if(id==1){
      that.setData({
        shippingMethodActive: 1,
        shipping_method: '门店自提',
        dTotal: that.data.dTotal - that.data.yunfei,//减运费
      }, function () {
      })
    }else if(id==2){
      that.setData({
        shippingMethodActive: 2,
        shipping_method: '邮寄到家',
        dTotal: that.data.dTotal + that.data.yunfei,//加运费
      }, function () {
      })
    }
  },
  //订单页面点击进入地址列表
  addAddress:function(){
    wx.navigateTo({
      url: '/pages/personal/address/address?isorderpage=1',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})